﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Origin2.Core;
using XNAssist;

namespace Origin2.Framework
{
    /// <summary>
    /// Represents a game "state", like a menu, message box, or anything similar
    /// </summary>
    public class GameState : IObject, IOriginUpdateable, IOriginDrawable, IVariableContainer, IControllable, IObjectContainer
    {
        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="parent"> The IObjectContainer containing this game state </param>
        public GameState(IObjectContainer parent)
        {
            Parent = parent;
            _variables = new Dictionary<string, object>();
            _objects = new BufferedDictionary<string, IObject>();
            _controls = new BufferedDictionary<string, Control>();
        }

        public virtual void Update(GameTime gameTime)
        {
            #region Game Object Updates

            // Update all of the IObjects that are also IUpdateables
            foreach (var pair in _objects)
            {
                var value = pair.Value as IOriginUpdateable;
                if (value != null)
                {
                    var cast = value;
                    cast.Update(gameTime);
                }

                if (pair.Value.MarkedForDeletion) _objects.Remove(pair.Key);
            }

            // Apply the buffer to the dictionary
            _objects.Update();

            #endregion

            #region Control Updates

            // Update all of the controls
            foreach (var pair in _controls)
            {
                pair.Value.Update(gameTime);
            }

            // Apply the buffer to the dictionary
            _controls.Update();

            #endregion
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            #region Draw Objects
            
            foreach (var drawable in _objects.Select(x => x.Value).OfType<IOriginDrawable>())
            {
                drawable.Draw(spriteBatch);
            }

            #endregion

            #region Draw Controls

            foreach (var pair in _controls)
            {
                pair.Value.Draw(spriteBatch);
            }

            #endregion
        }

        #region IObject Implementation

        public IObjectContainer Parent { get; private set; }

        public bool MarkedForDeletion { get; private set; }

        public void Delete()
        {
            MarkedForDeletion = true;
        }

        #endregion

        #region IVariableContainer Implementation

        private Dictionary<string, object> _variables;

        public void SetVariable(string key, object value)
        {
            if (_variables.ContainsKey(key))
                _variables[key] = value;
            else
                _variables.Add(key, value);
        }

        public object GetVariable(string key)
        {
            return _variables[key];
        }

        public TVariableType GetVariable<TVariableType>(string key)
        {
            return (TVariableType)GetVariable(key);
        }

        public void RemoveVariable(string key)
        {
            _variables.Remove(key);
        }

        public bool ContainsVariable(string key)
        {
            return _variables.ContainsKey(key);
        }

        public bool ContainsVariable(object value)
        {
            return _variables.ContainsValue(value);
        }

        #endregion

        #region IObjectContainer Implementation

        private BufferedDictionary<string, IObject> _objects;

        public void AddObject(string name, IObject obj)
        {
            _objects.Add(name, obj);
        }

        public IObject GetObject(string name)
        {
            return _objects[name];
        }

        public void RemoveObject(string name)
        {
            _objects.Remove(name);
        }

        public bool ContainsObject(string name)
        {
            return _objects.ContainsKey(name);
        }

        public bool ContainsObject(IObject obj)
        {
            return _objects.ContainsValue(obj);
        }

        public string GetUniqueName(string baseName)
        {
            if (!ContainsObject(baseName)) return baseName;

            var count = 0;

            while (true)
            {
                if (!ContainsObject(baseName + count)) return baseName + count;

                count++;
            }
        }

        #endregion

        #region IControllable Implementation

        private BufferedDictionary<string, Control> _controls;

        public void AddControl(string name, Control control)
        {
            _controls.Add(name, control);
        }

        public Control GetControl(string name)
        {
            return _controls[name];
        }

        public void RemoveControl(string name)
        {
            _controls.Remove(name);
        }

        public bool ContainsControl(string name)
        {
            return _controls.ContainsKey(name);
        }

        public bool ContainsControl(Control control)
        {
            return _controls.ContainsValue(control);
        }

        public bool ContainsControlType<TControlType>()
        {
            return _controls.Any(x => x.Value.GetType() == typeof(TControlType));
        }

        #endregion
    }
}
